package com.baizhi.springboot.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baizhi.springboot.annotation.LogAnnotation;
import com.baizhi.springboot.dao.GuruDao;
import com.baizhi.springboot.entity.Guru;
import com.baizhi.springboot.service.GuruService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class GuruServiceImpl implements GuruService {
    @Autowired
    private GuruDao guruDao;
    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询所有上师信息分页",type = "查询操作")
    public Map selectGuruByPage(Integer page, Integer limit) {
        Integer offset=(page-1)*limit;
        List<Guru> gurus = guruDao.selectGuruByPage(offset, limit);
        Integer count = guruDao.selectCount();
        /* "code": 0,
          "msg": "",
          "count": 1000,
          "data": [{}, {}]*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", gurus);
        return map;
    }

    @Override
    @LogAnnotation(content = "添加上师",type = "添加操作")
    public void insertGuru(Guru guru) {
        guruDao.insertGuru(guru);
    }

    @Override
    @LogAnnotation(content = "删除上师",type = "删除操作")
    public void deleteGuru(Integer id) {
        guruDao.deleteGuru(id);
    }

    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询单个上师",type = "查询操作")
    public Guru selectOneGuru(Integer id) {
        return guruDao.selectOneGuru(id);
    }

    @Override
    @LogAnnotation(content = "修改上师",type = "修改操作")
    public void updateGuru(Guru guru) {
        guruDao.updateGuru(guru);
    }

    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询所有上师",type = "查询操作")
    public List<Guru> selectAllGuru() {
        return guruDao.selectAllGuru();
    }

    @Override
    @LogAnnotation(content = "导入上师excel",type = "导入操作")
    public HSSFWorkbook daochuGuru() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Guru> gurus = guruDao.selectAllGuru();
        /*for (Guru guru : gurus) {
            System.out.println(guru.getGuruImage());
        }*/
        ExportParams entity=new ExportParams("上师展示","sheet", ExcelType.HSSF);
        return (HSSFWorkbook) ExcelExportUtil.exportExcel(entity, Guru.class, gurus);
    }

    @Override
    @LogAnnotation(content = "导出上师excel",type = "导出操作")
    public void daoruGuru(InputStream inputStream) throws Exception {
        ImportParams params=new ImportParams();
        params.setTitleRows(1);
        List<Guru> list = ExcelImportUtil.importExcel(inputStream, Guru.class, params);
        for (Guru guru : list) {
            int i = guru.getGuruImage().indexOf("\\");
            System.out.println(guru.getGuruImage().substring(i));
            guru.setGuruImage(guru.getGuruImage().substring(i));
        }
        guruDao.daoruGuru(list);
    }

    /*@Override
    public HSSFWorkbook daochuGuru() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("guru");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("guru_id");
        HSSFCell cella = row.createCell(1);
        cella.setCellValue("guru_name");
        HSSFCell cellb = row.createCell(2);
        cellb.setCellValue("guru_image");
        HSSFCell cellc = row.createCell(3);
        cellc.setCellValue("guru_nickname");
        HSSFCell celld = row.createCell(4);
        celld.setCellValue("guru_status");
        List<Guru> gurus = guruDao.selectAllGuru();
        Integer number=1;
        for (Guru guru : gurus) {
            HSSFRow row1 = sheet.createRow(number);
            Field[] fields = Guru.class.getDeclaredFields();
            for (int i=0;i<fields.length;i++){
                String methodName = "get" + fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1);
                Method method = Guru.class.getMethod(methodName);
                Object invoke = method.invoke(guru);
                HSSFCell cell1 = row1.createCell(i);
                cell1.setCellValue(invoke+"");
            }
            number++;
        }
        return workbook;
    }*/
}
